﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Threading;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace PiggyBank
{
    public enum EmGoldType
    {
        Gold,Silver
    }
	public partial class Gold : UserControl,IDisposable
	{
        
        DispatcherTimer selfloop = new DispatcherTimer();
		public Gold()
		{
			// Required to initialize variables
			InitializeComponent();
            selfloop.Tick += new EventHandler(selfloop_Tick);
            selfloop.Interval = TimeSpan.FromMilliseconds(33);
            ANI_Move.Completed += new EventHandler(ANI_Move_Completed);
		}
        private EmGoldType thisGoldType = EmGoldType.Silver;

        public EmGoldType ThisGlodType
        {
            get { return thisGoldType; }
            set 
            {
                thisGoldType = value;
                if (EmGoldType.Gold == value)
                {
                    for (int i = 0; i < LayoutRoot.Children.Count; i++)
                    {
                        Image img = LayoutRoot.Children[i] as Image;
                        if (img != null)
                            img.Source = new BitmapImage(new Uri("/PiggyBank;component/Res/money/m1" + i + ".png", UriKind.Relative));

                    }
                }
            }
        }

        bool isEnd = false;
        void ANI_Move_Completed(object sender, EventArgs e)
        {
            isEnd = true;
            
        }
        bool isClip = false;
        public bool IsD(Point pt)
        {
            if (isClip)
                return false;
            Rect rt = new Rect(X, Y + JumpEndY, 64, 64);
            return rt.Contains(pt);
            
        }
        public double X
        {
            get {return  Canvas.GetLeft(this); }
            set { Canvas.SetLeft(this, value); }
        }
        public double Y
        {
            get { return Canvas.GetTop(this); }
            set { Canvas.SetTop(this, value); }
        }
        public double Speed { get; set; }
        void selfloop_Tick(object sender, EventArgs e)
        {
            if(isEnd == false)
                X += Speed;

            foreach (var item in this.LayoutRoot.Children)
            {
                item.Visibility = System.Windows.Visibility.Collapsed;
            }
            LayoutRoot.Children[iframe].Visibility = System.Windows.Visibility.Visible;
            iframe += 1;
            if(iframe >=10)
                iframe = 0;
        }
        int iframe = 0;
        public void BeginMove()
        {
            isEnd = false;
            selfloop.Start();
            ANI_Move.Begin();
        }
        public double TargetX
        {
            set { this.targetX.Value = value; }
        }
        public double TargetY
        {
            set { this.targetY.Value = value; }
        }
        public void BeginMoveTo(double x, double y)
        {
            TargetX = x;
            TargetY = y;
            ANI_MoveTO.Completed += new EventHandler(ANI_MoveTO_Completed);
            ANI_MoveTO.Begin();
            isClip = true;
        }

        void ANI_MoveTO_Completed(object sender, EventArgs e)
        {
            if (OnMoveToEnd != null)
                OnMoveToEnd(this);
        }
        public double JumpEndY 
        {
            get { return jumpEndY.Value; }
            set { jumpEndY.Value = value; }
        }
        public void Dispose()
        {
            OnClick = null;
            OnMoveToEnd = null;
            foreach (var item in this.LayoutRoot.Children)
            {
                var image = item as Image;
                if(image!=null)
                    image.Source = null;
            }
            ANI_MoveTO.Stop();
            ANI_MoveTO.Completed -=ANI_MoveTO_Completed;
            OnClick = null;
            ANI_Move.Stop();
            ANI_Move.Completed -= ANI_Move_Completed;
            selfloop.Stop();
            selfloop.Tick -= selfloop_Tick;
            selfloop = null;
        }
        public Action<Gold> OnClick;

        public Action<Gold> OnMoveToEnd;

        private void Gold_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	
            
            
        }
    }
}